{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取当前工作路径\n",
    "工作路径是程序在文件系统中的当前操作起点，相对路径是相对于这个起点来解析的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PosixPath('/home/jdz/Documents/CodeData/Code/Python/BaseStudyCode/GrammerStudy')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cwd = Path.cwd()\n",
    "cwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 父目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PosixPath('/home/jdz/Documents/CodeData/Code/Python/BaseStudyCode')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cwd.parent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 判断文件夹或者文件是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cwd.exists()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 判断是否是文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cwd.is_file()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 判断是否是文件夹"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cwd.is_dir()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建文件夹\n",
    "* parents=True 创建多级目录\n",
    "* exist_ok=True 若目录已存在则不报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_dir = cwd / 'test_dir'\n",
    "(test_dir).mkdir(exist_ok=True)\n",
    "# test_dir.rmdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建多级目录\n",
    "test_dir = cwd / 't1' / 't2' / 't3'\n",
    "test_dir.mkdir(parents=True, exist_ok=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除文件夹\n",
    "* rmdir() 目录不为空时，删除目录, 否则报错\n",
    "* 与shutil 结合 用rmtree() 递归删除目录及其内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_dir.rmdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "shutil.rmtree(cwd / 't1')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_txt = test_dir / 'test.txt' # 定义文件路径\n",
    "test_txt.touch() # 创建文件\n",
    "test_txt.exists()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除文件\n",
    "* 如果文件不存在会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_txt.unlink()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取文件主干名和扩展名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_txt.stem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'.txt'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_txt.suffix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## rglob 递归遍历符合通配符的文件或文件夹"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01.py\n",
      "mylog.py\n",
      "01.py\n",
      "2.py\n",
      "is_deep_copy.py\n",
      "main.py\n",
      "decorator.py\n",
      "三线表.py\n",
      "note_img.py\n",
      "mkdir_file.py\n",
      "rename.py\n",
      "project_struct.py\n",
      "blog_spider.py\n",
      "mix.py\n",
      "common.py\n",
      "1.py\n",
      "join.py\n",
      "mul.py\n",
      "pool.py\n",
      "map.py\n",
      "daemon.py\n",
      "mul_thread_order.py\n",
      "mul_task.py\n",
      "mul_thread_args.py\n",
      "customize_thread.py\n",
      "queue_use.py\n",
      "mul_thread_excute_task.py\n",
      "use_timer.py\n",
      "use_sched.py\n",
      "thread_pool_study.py\n",
      "lock_concurrent.py\n",
      "mul_thread.py\n",
      "mul_aio.py\n",
      "1.py\n",
      "task.py\n",
      "event_loop.py\n",
      "set_await.py\n",
      "coroutines.py\n",
      "futures.py\n",
      "tasks.py\n",
      "2.py\n",
      "1.py\n",
      "asyncio_run.py\n",
      "asyncio_create_task_function.py\n",
      "awaiting_coroutine.py\n",
      "2.py\n",
      "asyncio_wait.py\n",
      "test.py\n",
      "pool.py\n",
      "pro_con.py\n",
      "quque.py\n",
      "2.py\n",
      "1.py\n",
      "3.py\n",
      "5.insert.py\n",
      "4-zip-enumerate.py\n",
      "1.py\n",
      "sort.py\n",
      "slice.py\n",
      "test.py\n",
      "bitwise-or.py\n",
      "sort.py\n",
      "is_order.py\n",
      "1.py\n",
      "cq2.py\n",
      "createQuestion.py\n",
      "series.py\n",
      "02.py\n",
      "03.py\n",
      "01.py\n",
      "JsonToExcel.py\n",
      "JsonToExcel.py\n",
      "JsonToExcel.py\n",
      "03.py\n",
      "base_filter.py\n",
      "scope.py\n",
      "socket_server.py\n",
      "socket_problem.py\n",
      "ip_port.py\n",
      "socket_client.py\n",
      "concurrent_future.py\n",
      "thread_queue.py\n",
      "producer_consumer_executor.py\n",
      "thread_sync.py\n",
      "producer_consumer.py\n",
      "stu_thread.py\n",
      "01.py\n",
      "01.py\n",
      "op.py\n",
      "abstract.py\n",
      "date_stu.py\n",
      "1.py\n",
      "fun_args.py\n",
      "mul_return_val.py\n",
      "lambda.py\n",
      "01.py\n",
      "args.py\n",
      "reduce.py\n",
      "any_.py\n",
      "zip.py\n",
      "sorted.py\n",
      "filter_.py\n",
      "repeat.py\n",
      "map.py\n",
      "all_.py\n",
      "18-面向对象-封装.py\n",
      "t.py\n",
      "19-类型注解.py\n",
      "17-多变量for循环.py\n",
      "18-面向对象-继承.py\n",
      "small-num.py\n",
      "23-platform.py\n",
      "02.py\n",
      "10-function.py\n",
      "09-current.py\n",
      "08-dict.py\n",
      "20.py\n",
      "05-str.py\n",
      "25-match_case.py\n",
      "06-序列和切片.py\n",
      "24-unpack.py\n",
      "16-pyecharts.py\n",
      "21enum.py\n",
      "14-package.py\n",
      "04-tuple.py\n",
      "07-set.py\n",
      "12-file_write.py\n",
      "01.py\n",
      "26-lazy_func.py\n",
      "22-date.py\n",
      "11-file-open_read.py\n",
      "13-try-except.py\n",
      "15-json.py\n",
      "18-面向对象-多态.py\n",
      "03-list.py\n",
      "loop-else.py\n",
      "auto.py\n",
      "flag.py\n",
      "logic_op.py\n",
      "bitwise_or.py\n",
      "1.py\n",
      "max_index.py\n",
      "_self.py\n",
      "magic_method.py\n",
      "_construct_method.py\n",
      "01.py\n",
      "obj_bool.py\n",
      "pratice.py\n",
      "_constant.py\n",
      "_fake_private.py\n",
      "Main.py\n",
      "test.py\n",
      "__init__.py\n",
      "file_path.py\n",
      "合并区间.py\n",
      "最长公共前缀.py\n",
      "旋转矩阵.py\n",
      "移除元素.py\n",
      "统计子串.py\n",
      "合并两个有序数组.py\n"
     ]
    }
   ],
   "source": [
    "for f in cwd.rglob(\"*.py\"):\n",
    "    print(f.name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "jupter-env-10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
